package com.ruoyi.common.core.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Author:cgChen
 * Date:2022/05/17 10:21
 * Description:
 */
@Component
public class SendSmsUtils {
    private static Logger log = LoggerFactory.getLogger(SendSmsUtils.class);
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
     private static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送短信
     * @param accessKeyId
     * @param accessKeySecret
     * @param phoneNumber
     * @param signName
     * @param templateCode
     * @param templateParam
     * @return
     * @throws Exception
     */
    public static Map<String,String> send(String accessKeyId,String accessKeySecret,String phoneNumber,
                                           String signName,String templateCode,String templateParam) throws Exception {
        Map<String,String> map=new HashMap<>();
        com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient(accessKeyId, accessKeySecret);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumber)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam(templateParam);
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sendResp = client.sendSms(sendSmsRequest);
        String code = sendResp.body.code;
        map.put(code,sendResp.body.toString());
        if (!com.aliyun.teautil.Common.equalString(code, "OK")) {
            log.error("错误信息: " + sendResp.body.message + "");
        }
        return map;
    }

    public static void main(String[] args_) throws Exception {
        String accessKeyId = "LTAI4GCToHsMvkmnwQaMbDgG";
        String accessKeySecret = "CZqwSub23ao1XCKRw81ehPz3l7Yxvl";
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient(accessKeyId, accessKeySecret);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers("15071272007")
                .setSignName("荣耀功夫科技有限公司")
                .setTemplateCode("SMS_200178785")
                .setTemplateParam("{\"code\":\"132456\"}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sendResp = client.sendSms(sendSmsRequest);
        String code = sendResp.body.code;
        if (!com.aliyun.teautil.Common.equalString(code, "OK")) {
            log.error("错误信息: " + sendResp.body.message + "");
            return;
        }
    }

    /**
     * 创建指定数量的随机字符串
     * @param numberFlag 是否是数字
     * @param length
     * @return
     */
    public static String createRandom(boolean numberFlag, int length){
        String retStr = "";
        String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);
        return retStr;
    }
}
